package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.GradeLevelDto;
import io.geekidea.boot.shop.entity.GradeLevel;
import io.geekidea.boot.shop.mapper.GradeLevelMapper;
import io.geekidea.boot.shop.query.app.AppGradeLevelQuery;
import io.geekidea.boot.shop.query.web.GradeLevelQuery;
import io.geekidea.boot.shop.service.base.api.GradeLevelService;
import io.geekidea.boot.shop.vo.app.AppGradeLevelVo;
import io.geekidea.boot.shop.vo.web.GradeLevelVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 11个等级 服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class GradeLevelServiceImpl extends ServiceImpl<GradeLevelMapper, GradeLevel> implements GradeLevelService {

    @Autowired
    private GradeLevelMapper gradeLevelMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addGradeLevel(GradeLevelDto dto) {
        GradeLevel gradeLevel = new GradeLevel();
        BeanUtils.copyProperties(dto, gradeLevel);
        return save(gradeLevel);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGradeLevel(GradeLevelDto dto) {
        Long id = dto.getId();
        GradeLevel gradeLevel = getById(id);
        if (gradeLevel == null) {
            throw new BusinessException("11个等级不存在");
        }
        BeanUtils.copyProperties(dto, gradeLevel);
        return updateById(gradeLevel);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteGradeLevel(Long id) {
        return removeById(id);
    }

    @Override
    public GradeLevelVo getGradeLevelById(Long id) {
        return gradeLevelMapper.getGradeLevelById(id);
    }

    @Override
    public Paging<GradeLevelVo> getGradeLevelPage(GradeLevelQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<GradeLevelVo> list = gradeLevelMapper.getGradeLevelPage(query);
        Paging<GradeLevelVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppGradeLevelVo getAppGradeLevelById(Long id) {
        return gradeLevelMapper.getAppGradeLevelById(id);
    }

    @Override
    public Paging<AppGradeLevelVo> getAppGradeLevelPage(AppGradeLevelQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppGradeLevelVo> list = gradeLevelMapper.getAppGradeLevelPage(query);
        Paging<AppGradeLevelVo> paging = new Paging<>(list);
        return paging;
    }

}
